package com.example.demo.controller;

import com.example.demo.model.LegalPerson;
import com.example.demo.model.User;
import com.example.demo.model.vo.*;
import com.example.demo.repository.LegalPersonReposirtory;
import com.example.demo.service.LegalPersonService;
import com.example.demo.service.PlantService;
import com.example.demo.util.AllRecords;
import com.example.demo.util.Result;
import com.example.demo.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * Created by Lx on 2018/6/27.
 */
@RestController
public class LegalPersonController {

    @Autowired
    private LegalPersonReposirtory legalPersonPlantReposirtory;

    @Autowired
    private LegalPersonService legalPersonService;

    @Autowired
    private PlantService plantService;

    @GetMapping("legalPersonPlant/queryById")
    public Result<List<LegalPerson>> queryById(Integer id){
      return ResultVO.success(legalPersonPlantReposirtory.findByUserId(id));
    }

    @PostMapping("/legalPersonPlant/addPlant")
    public Result<PlantAddVo> addPlant(@RequestBody @Valid PlantAddVo PlantAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return ResultVO.error(1,bindingResult.getFieldError().getDefaultMessage());
        }
        return plantService.addPlant(PlantAddVo);
    }

    @PostMapping("/legalPersonPlant/updatePlant")
    public Result<PlantUpdateVo> updatePlant(@RequestBody @Valid PlantUpdateVo plantUpdateVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return ResultVO.error(1,bindingResult.getFieldError().getDefaultMessage());
        }
        return plantService.updatePlant(plantUpdateVo);
    }

    @PostMapping("/legalPersonPlant/queryPage")
    public Result<AllRecords<PlantVo>> queryPage(@RequestBody PlantCerteriaVo plantCerteriaVo){
        return plantService.queryPage(plantCerteriaVo);
    }
    /**
     *@Valid此注解验证传过来的参数
     * @RequestBody此注解是Post的接收方式
     */
    @PostMapping("/legalPersonPlant/addLegalPerson")
    public Result<LegalPersonAddVo> addLegalPerson(@RequestBody @Valid LegalPersonAddVo LegalPersonAddVo, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            //通过BindingResult拿到我们在实体类注解里面的错误信息
            System.out.println(bindingResult.getFieldError().getDefaultMessage());
            return ResultVO.error(1,bindingResult.getFieldError().getDefaultMessage());
        }
        return  legalPersonService.addLegalPerson(LegalPersonAddVo);
    }

    /**
     * 根据法人查询用户信息信息
     */
    @GetMapping("legalPersonPlant/queryByLegalPersonId")
    public List<User> queryByLegalPersonId(@RequestParam("id") Integer id){

        LegalPerson one = legalPersonPlantReposirtory.findOne(id);
        System.out.println(one.getUser().getCode());
        System.out.println(one.getUser().getPassword());
        return null;
    }
}
